//
//  UIView+Tips.m
//  iOSLib
//
//  Created by gaofeng on 15/11/24.
//  Copyright © 2015年 zyb. All rights reserved.
//

#import "UIView+Tips.h"
#import "TipsTextManager.h"
#import "UIView+Gesture.h"
#import "UIColor+Hex.h"
#import "MBProgressHUD.h"
#import "Reachability.h"
#import "ZYBConfigInfo.h"
#import <objc/runtime.h>
#import "UIImage+Additions.h"

static const char imageNameScheme;

@implementation UIView (Tips)

- (TipsView *)showTipsWithTipsTextModel:(TipsTextModel *)model imagePath:(NSString *)path tapCallBack:(TipsViewTouchedCallBack)callBack frame:(CGRect)frame {
    TipsView *tipView = [self viewWithTag:VIEW_TIP_VIEW_TAG];
    if(path){
        objc_setAssociatedObject(self, &imageNameScheme, model.imageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", model.imageName]];
        if (SYSTEM_VERSION_LESS_THAN(@"8.0")) {
            path = [path stringByAppendingString:@"@2x.png"];
        }
        else if ([[UIScreen mainScreen] scale] ==1){
            path = [path stringByAppendingString:@"@2x.png"];
        }
        if ([UIImage imageWithContentsOfFile:path]) {
            model.image = [UIImage imageWithContentsOfFile:path];
        }
        else{
            model.image =[UIImage imageNamed:model.imageName];
            
        }
    } else {
        objc_setAssociatedObject(self, &imageNameScheme, model.imageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        model.image = model.image?:[UIImage imageNamed:model.imageName];
    }
    
    if (!tipView) {
        tipView = [[TipsView alloc] initWithFrame:frame];
        [tipView addTapGestureWithTarget:self selector:@selector(tipViewTouched)];
        tipView.tag = VIEW_TIP_VIEW_TAG;
    }
    tipView.frame = frame;
    tipView.callBack = callBack;
    tipView.model = model;

    
    [self addSubview:tipView];
    [self bringSubviewToFront:tipView];
    
    return tipView;
}

- (TipsView *)showTipsWithTipsTextModel:(TipsTextModel *)model imagePath:(NSString *)path tapCallBack:(TipsViewTouchedCallBack)callBack frame:(CGRect)frame resizeImageSize:(CGSize)imageSize
{
    TipsView *tipView = [self viewWithTag:VIEW_TIP_VIEW_TAG];
    if(path){
        objc_setAssociatedObject(self, &imageNameScheme, model.imageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", model.imageName]];
        if (SYSTEM_VERSION_LESS_THAN(@"8.0")) {
            path = [path stringByAppendingString:@"@2x.png"];
        }
        else if ([[UIScreen mainScreen] scale] ==1){
            path = [path stringByAppendingString:@"@2x.png"];
        }
        if ([UIImage imageWithContentsOfFile:path]) {
            model.image = [UIImage imageWithContentsOfFile:path];
        }
        else{
            model.image =[UIImage imageNamed:model.imageName];
            
        }
    } else {
        objc_setAssociatedObject(self, &imageNameScheme, model.imageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        model.image =[UIImage imageNamed:model.imageName];
    }
    
    CGFloat resizeWidth = 0.f;
    CGFloat resizeHeight = 0.f;
    if (imageSize.width >0) {
        resizeHeight = imageSize.width * model.image.size.height/model.image.size.width;
        resizeWidth = imageSize.width;
    }else if(imageSize.height>0){
        resizeWidth = imageSize.height * model.image.size.width/model.image.size.height;
        resizeHeight = imageSize.height;
    }
    
    UIImage *resizeImage = [UIImage scaleImage:model.image withNewSize:CGSizeMake(resizeWidth, resizeHeight)];
    model.image = resizeImage;
    if (!tipView) {
        tipView = [[TipsView alloc] initWithFrame:frame];
        [tipView addTapGestureWithTarget:self selector:@selector(tipViewTouched)];
        tipView.tag = VIEW_TIP_VIEW_TAG;
    }
    tipView.frame = frame;
    tipView.callBack = callBack;
    tipView.model=model;
    [tipView setNeedsLayout];
    
    [self addSubview:tipView];
    [self bringSubviewToFront:tipView];
    
    return tipView;
}

- (TipsView *)showTipsWithType:(ZYBViewTipsType)tipType imagePath:(NSString *)path tapCallBack:(TipsViewTouchedCallBack)callBack frame:(CGRect)frame {
    TipsTextModel *model = [TipsTextManager tipsModelForType:tipType];
    return  [self showTipsWithTipsTextModel:model imagePath:path tapCallBack:callBack frame:frame];
}

- (TipsView *)showTipsWithType:(ZYBViewTipsType)tipType imagePath:(NSString *)path tapCallBack:(TipsViewTouchedCallBack)callBack frame:(CGRect)frame resizeImageSize:(CGSize)resize
{
    TipsTextModel *model = [TipsTextManager tipsModelForType:tipType];
    return  [self showTipsWithTipsTextModel:model imagePath:path tapCallBack:callBack frame:frame resizeImageSize:resize];
}

- (TipsView *)showTipsWithTipsType:(ZYBViewTipsType)tipType imagePath:(NSString *)path tapCallBack:(TipsViewTouchedCallBack)callBack {
    return [self showTipsWithType:tipType imagePath:path tapCallBack:callBack frame:self.bounds];
}

- (TipsView *)showTipsWithTipsType:(ZYBViewTipsType)tipType imagePath:(NSString *)path tapCallBack:(TipsViewTouchedCallBack)callBack offset:(CGFloat)offset {
    return [self showTipsWithType:tipType imagePath:path tapCallBack:callBack frame:CGRectMake(0, offset, self.bounds.size.width, self.bounds.size.height - offset)];
}

- (TipsView *)showTipsWithImageName:(NSString *)imageName tapCallBack:(TipsViewTouchedCallBack)callBack offset:(CGFloat)offset {
    TipsTextModel *model = [[TipsTextModel alloc] init];
    model.title = nil;
    model.type = ZYBViewTipsTypeDefault;
    model.imageName = imageName;
    model.image = [UIImage imageNamed:imageName];
    return [self showTipsWithTipsTextModel:model imagePath:nil tapCallBack:callBack frame:CGRectMake(0, offset, self.bounds.size.width, self.bounds.size.height - offset)];
}

- (TipsView *)showLoadFailedTipWithTapCallBack:(TipsViewTouchedCallBack)callBack imagePath:(NSString *)path {
    ZYBViewTipsType style = ZYBViewTipsTypeLoadError;
    if ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
        style = ZYBViewTipsTypeNetworkError;
    }
    return [self showTipsWithTipsType:style imagePath:path tapCallBack:callBack];
}

- (TipsView *)showTipsWithTipsType:(ZYBViewTipsType)tipType imagePath:(NSString *)path {
    return [self showTipsWithTipsType:tipType imagePath:path tapCallBack:nil];
}

- (void)dismissTipsView {
    UIView *tipView = [self viewWithTag:VIEW_TIP_VIEW_TAG];
    if (tipView != nil) {
        [tipView removeFromSuperview];
    }
}

- (TipsView *)getTipsView {
    return [self viewWithTag:VIEW_TIP_VIEW_TAG];
}

- (void)tipViewTouched {
    TipsView *tipView = [self viewWithTag:VIEW_TIP_VIEW_TAG];
    
    if (tipView.callBack) {
        tipView.callBack(tipView.tipType);
    }
}
- (TipsView *)showTipsWithTipsType:(ZYBViewTipsType)tipType{
    return [self showTipsWithTipsType:tipType imagePath:nil];
}

- (TipsView *)showTipsWithTipsType:(ZYBViewTipsType)tipType tapCallBack:(TipsViewTouchedCallBack)callBack{
    return [self showTipsWithTipsType:tipType imagePath:nil tapCallBack:callBack];
}
- (TipsView *)showTipsWithTipsType:(ZYBViewTipsType)tipType tapCallBack:(TipsViewTouchedCallBack)callBack offset:(CGFloat)offset{
    return [self showTipsWithTipsType:tipType imagePath:nil tapCallBack:callBack offset:offset];
}

- (TipsView *)showTipsWithType:(ZYBViewTipsType)tipType tapCallBack:(TipsViewTouchedCallBack)callBack frame:(CGRect)frame {
    return [self showTipsWithType:tipType imagePath:nil tapCallBack:callBack frame:frame];
}

- (TipsView *)showLoadFailedTipWithTapCallBack:(TipsViewTouchedCallBack)callBack{
    return [self showLoadFailedTipWithTapCallBack:callBack imagePath:nil];
}

- (void)reloadTipImageViewWithCurrentPath:(NSString *)path{
    TipsView *tipView = [self viewWithTag:VIEW_TIP_VIEW_TAG];
    NSString *imageName = objc_getAssociatedObject(self, &imageNameScheme);
    if (path) {
        path = [path stringByAppendingPathComponent:imageName];
        if (SYSTEM_VERSION_LESS_THAN(@"8.0")) {
            path = [path stringByAppendingString:@"@2x.png"];
        }
        else if ([[UIScreen mainScreen] scale] ==1){
            path = [path stringByAppendingString:@"@2x.png"];
        }
        tipView.imageView.image = [UIImage imageWithContentsOfFile:path];
    }else{
        tipView.imageView.image = [UIImage imageNamed:imageName];
    }

}
@end

